package com.starter.swagger.autoconfigure;

import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;
import com.starter.swagger.properties.SwaggerConfigureProperties;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger.configuration.SwaggerCommonConfiguration;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import springfox.documentation.swagger2.configuration.Swagger2DocumentationConfiguration;
import springfox.documentation.swagger2.web.Swagger2Controller;

/**
 * @Description swagger自动装配类
 * @Author Dang Xianyue
 * @Date 2020-08-14 09:39
 * @Version 1.0
 */
@Configuration
@EnableSwagger2//通过@EnableSwagger2注解来启用Swagger2
@EnableSwaggerBootstrapUI
@ConditionalOnWebApplication
@ConditionalOnClass({SwaggerCommonConfiguration.class, Swagger2DocumentationConfiguration.class, Swagger2Controller.class})
@ConditionalOnProperty(prefix = "com.starter.swagger",name = "enabled",havingValue = "true")
@EnableConfigurationProperties({SwaggerConfigureProperties.class})
public class SwaggerAutoConfigure implements WebMvcConfigurer {

    @Autowired
    private SwaggerConfigureProperties swaggerConfigureProperties;

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo(swaggerConfigureProperties.getApiInfo()))
                .select()
                //为当前包路径
                .apis(RequestHandlerSelectors.basePackage(swaggerConfigureProperties.getBasePackage()))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo(com.starter.swagger.properties.ApiInfo apiInfo) {
        return new ApiInfoBuilder()
                //标题
                .title(StringUtils.isNotBlank(apiInfo.getTitle())?apiInfo.getTitle():"swagger-bootstrap-ui RESTful APIs")
                //详情
                .description(StringUtils.isNotBlank(apiInfo.getDescription())?apiInfo.getDescription():"swagger-bootstrap-ui")
                //访问地址
                .termsOfServiceUrl(StringUtils.isNotBlank(apiInfo.getServiceUrl())?apiInfo.getServiceUrl():"http://localhost:8080/")
                //版本
                .version(StringUtils.isNotBlank(apiInfo.getVersion())?apiInfo.getVersion():"1.0")
                .build();
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
}
